<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/8/28
 * Time: 15:20
 */

namespace app\apiteacher\controller;


use think\Db;

class My extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 编辑/完善用户信息
     * @return \think\response\Json
     * user_image      头像
     * user_username   名称
     * user_gender     性别   1 男   2 女
     * user_birthday   生日
     * sheng           省
     * shi             市
     * qu              区
     * email           邮箱
     * user_intro      简介
     * user_biye_image 毕业证
     * user_edu_id     学历
     */
    public function changeUserInfo(){
        $user_image = input('user_image');
        $user_username = input('user_username');
        $user_gender = input('user_gender');
        $user_birthday = input('user_birthday');
        $sheng = input('sheng');
        $shi = input('shi');
        $qu = input('qu');
        $user_email = trim(input('email'), ',');
        $user_intro = trim(input('user_intro'), ' ');
        $user_biye_image = input('user_biye_image');
        $user_edu_id = input('user_edu_id');
        $user_district_str = $this->areaStr($sheng, $shi, $qu);
        
        $data = [
            'user_image' => $user_image,
            'user_username' => $user_username,
            'user_gender' => $user_gender,
            'user_birthday' => $user_birthday,
            'user_district_str' => $user_district_str,
            'user_email' => $user_email,
            'user_intro' => $user_intro,
            'user_sheng' => $sheng,
            'user_shi' => $shi,
            'user_qu' => $qu,
            'user_biye_image' => $user_biye_image,
            'user_edu_id' => $user_edu_id,
            'user_update_time' => date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($data);
        if($res){
            return json(['status' => 200, 'msg' => '保存成功']);
        }else{
            return json(['status' => 0, 'msg' => '保存失败']);
        }
    }
    
    /**
     * 提交审核申请
     * @return \think\response\Json
     * subject_id          课程ID
     * grade_id            年级ID
     * user_honor_img      荣誉证书   非必填
     * user_good_at        擅长知识点
     * user_tcahe_style    教学特色
     * user_toll           每小时收费金额
     * user_front_img      身份证正面
     * user_back_img       身份证反面
     * user_zige_img       教师资格证书
     */
    public function submitCert(){
        $subject_id = input('subject_id');
        $grade_id = input('grade_id');
        $user_good_at = input('user_good_at');
        $user_tcahe_style = input('user_tcahe_style');
        $user_toll = input('user_toll');
        $user_front_img = input('user_front_img');
        $user_back_img = input('user_back_img');
        $user_zige_img = input('user_zige_img');
        $user_honor_img = input('user_honor_img');
        $update = [
            'user_subject' => $subject_id,
            'user_grade' => $grade_id,
            'user_good_at' => $user_good_at,
            'user_tcahe_style' => $user_tcahe_style,
            'user_toll' => $user_toll,
            'user_front_img' => $user_front_img,
            'user_back_img' => $user_back_img,
            'user_zige_img' => $user_zige_img,
            'user_status' => 1
        ];
        $update['user_honor_img'] = $user_honor_img;
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '选择成功']);
        }else{
            return json(['status' => 0, 'msg' => '选择失败']);
        }
    }
    
    /**
     * 老师端用户协议
     * @return \think\response\Json
     */
    public function registerAgreement(){
        $agreement = Db::table('up_about')->where('about_id', 9)->value('about_teacher_agreement');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 获取省市区的三维数组
     * @return \think\response\Json
     */
    public function getArea()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $provinceLists = Db::name('area')->where('level', 1)->select();//省
        $cityLists = Db::name('area')->where('level', 2)->select();//市
        $districtLists = Db::name('area')->where('level', 3)->select();//区
        foreach($cityLists as $k => $city) {
            foreach($districtLists as $kk => $district) {
                if($city['district_id'] == $district['pid']) {
                    $cityLists[$k]['qu'][] = $district;
                }
            }
        }
        foreach($provinceLists as $key => $province) {
            foreach($cityLists as $ke => $city) {
                if($province['district_id'] == $city['pid']) {
                    $provinceLists[$key]['city'][] = $city;
                }
            }
        }
        return json(['status' => 200, 'msg' => $provinceLists]);
    }
    
    /**
     * 个人详情展示
     * @return \think\response\Json
     */
    public function showUserInfo(){
        $info = Db::table('up_u_user')
            ->join('up_grade', 'user_grade = grade_id', 'left')
            ->join('up_subject', 'user_subject = subject_id', 'left')
            ->join('up_edu', 'user_edu_id = edu_id', 'left')
            ->where('user_id', $this->user_id)
            ->field('user_image, user_username, user_gender, user_birthday, user_district_str, 
            grade_name, user_intro, subject_name, user_exper, user_email, user_status, user_grade, 
            user_subject, userid, edu_name, user_edu_id, user_biye_image')
            ->find();
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 推送开关
     * @return \think\response\Json
     */
    public function showSend(){
        $is_send = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_is_send');
        return json(['status' => 200, 'msg' => $is_send]);
    }
    
    /**
     * 打开/关闭消息推送开关
     * @return \think\response\Json
     */
    public function doSend(){
        $is_send = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_is_send');
        if($is_send == 1){
            $user_is_send = 2;
        }else{
            $user_is_send = 1;
        }
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->setField('user_is_send', $user_is_send);
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 更换手机号
     * @return \think\response\Json
     * phone  新的手机号
     * code   验证码
     * type   1 更换手机号第一步    2 更换手机号第二步
     */
    public function changePhone(){
        $phone = trim(input('phone'), ' ');
        $code = trim(input('code'), ' ');
        $type = input('type');
        if($type == 2){
            // 必填项
            if($phone == '' || $code == ''){
                return json(['status' => 0, 'msg' => '有未填项']);
            }
    
            // 手机号格式
            if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
                return json(['status' => 0, 'msg' => '请输入正确的手机号']);
            }
    
            $user = Db::table('up_u_user')->where('user_phone', $phone)->find();
            if($user){
                return json(['status' => 0, 'msg' => '该手机号已注册']);
            }
        }
        
        //验证验证码
        $res = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $res['code']) {
            return json(['status' => 0, 'msg' => '验证码错误']);
        }
        // 10分钟超时
        $now = time();
        if($now - $res['time'] >= 1801) {
            return json(['status' => 0, 'msg' => '验证码超时']);
        }
        if($res['status'] == 1) {
            return json(['status' => 0, 'msg' => '验证码已使用']);
        }
        
        if($type == 1){
            return json(['status' => 200, 'msg' => '通过验证']);
        }else{
            $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update(['user_phone' => $phone]);
            if($res){
                $userInfo = Db::table('up_u_user')->where('user_id', $this->user_id)->find();
                return json(['status' => 200, 'msg' => $userInfo]);
            }else{
                return json(['status' => 0, 'msg' => '更换失败']);
            }
        }
    }
    
    /**
     * 退出修改状态
     * @return \think\response\Json
     */
    public function loginOut(){
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->setField('user_online', 2);
        if($res){
            return json(['status' => 200, 'msg' => '退出成功']);
        }else{
            return json(['status' => 0, 'msg' => '退出失败']);
        }
    }
    
    /**
     * 我的钱包
     * @return \think\response\Json
     */
    public function myMoney(){
        $balance = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_balance');
        return json(['status' => 200, 'msg' => $balance]);
    }
    
    /**
     * 我的交易记录
     * @return \think\response\Json
     */
    public function myTransaction(){
        $tranList = Db::table('up_order')
            ->where('order_user_id', $this->user_id)
            ->field('order_type, order_price, order_time, order_up_down')
            ->order('order_time desc')
            ->page($this->page, 10)
            ->select();
        if($tranList){
            return json(['status' => 200, 'msg' => $tranList]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我的关注
     * @return \think\response\Json
     * page    当前页码
     */
    public function myAtt(){
        $list = Db::table('up_follow_user')
            ->join('up_u_user', 'user_id = follow_quilt_user_id', 'left')
            ->join('up_grade', 'user_grade = grade_id', 'left')
            ->join('up_subject', 'subject_id = user_subject', 'left')
            ->join('up_area', 'district_id = user_shi', 'left')
            ->field('user_username, user_image, user_gender, grade_name, user_id, 
            subject_name, district')
            ->where('follow_user_id', $this->user_id)
            ->order('user_sort desc')
            ->page($this->page, 10)
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我的公开课
     * @return \think\response\Json
     */
    public function myPublicClass(){
        $classList = Db::table('up_public_class')
            ->join('up_subject', 'subject_id = public_subject', 'left')
            ->field('public_id, subject_name, public_img, public_name, public_intro, public_type, 
            public_price')
            ->where('public_teacher_id', $this->user_id)
            ->order('public_is_top asc, public_time desc')
            ->page($this->page, 10)
            ->select();
        if($classList){
            // 报名人数
            foreach($classList as $k => $v){
                $count = Db::table('up_public_apply')->where('apply_public_id', $v['public_id'])->count();
                $classList[$k]['num'] = $count;
            }
            return json(['status' => 200, 'msg' => $classList]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 发布公开课时显示的年级和学科
     * @return \think\response\Json
     */
    public function gradeAndSubject(){
        $gradeSubject = Db::table('up_u_user')
            ->join('up_subject', 'subject_id = user_subject', 'left')
            ->join('up_grade', 'grade_id = user_grade', 'left')
            ->where('user_id', $this->user_id)
            ->field('user_grade, user_subject, subject_name, grade_name')
            ->find();
        return json(['status' => 200, 'msg' => $gradeSubject]);
    }
    
    /**
     * 老师所发布过的所有视频的总大小
     * @return \think\response\Json
     */
    public function myVideoSize(){
        $size = Db::table('up_public_class')
            ->join('up_public_section', 'section_public_id = public_id', 'left')
            ->where('public_teacher_id', $this->user_id)
            ->sum('section_size');
    
        if($size){
            $totle_size = round( $size = $size/1024, 2);
        }else{
            $totle_size = 0;
        }
        return json(['status' => 200, 'msg' => $totle_size]);
    }
    
    /**
     * 判断老师是否审核通过
     * @return \think\response\Json
     * type    判断类型     1 题库   2 公开课
     */
    public function teacherIsAllow(){
        $type = input('type');
        if($type == 1){
            $start_time = date('Y-m-d 00:00:00');
            $end_time = date('Y-m-d 23:59:59');
            $count = Db::table('up_question_bank')
                ->where('question_user_id', $this->user_id)
                ->where('question_from', 1)
                ->where('question_time', 'between', [$start_time, $end_time])
                ->count();
            if($count >= 5){
                return json(['status' => 0, 'msg' => '当天发布数量不可超过5条']);
            }
        }
        $status = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_status');
        return json(['status' => 200, 'msg' => $status]);
    }
    
    /**
     * 发布公开课
     * @return \think\response\Json
     */
    public function addPublicClass(){
        $class = input('classs');
        $class = json_decode($class, true);

        // 插入公开课表
        $class_insert = [
            'public_teacher_id' => $this->user_id,
            'public_name' => $class['public_name'],
            'public_subject' => $class['public_subject'],
            'public_grade' => $class['public_grade'],
            'public_intro' => $class['public_intro'],
            'public_img' => $class['public_img'],
            'public_time' => date('Y-m-d H:i:s')
        ];
    
        // 根据价格判断是否免费
        if($class['public_price']){
            $class_insert['public_type'] = 2;
            $class_insert['public_price'] = $class['public_price'];
        }else{
            $class_insert['public_type'] = 1;
            $class_insert['public_price'] = 0;
        }
    
        Db::startTrans();
        try{
            // 公开课
            $public_id = Db::table('up_public_class')->insertGetId($class_insert);
            // 节
            if(isset($class['section']) && is_array($class['section']) && !empty($class['section'])){
                foreach($class['section'] as $key => $vo){
                    $section_insert = [
                        'section_public_id' => $public_id,
                        'section_name' => $vo['section_name'],
                        'section_type' => $vo['section_type'], // 是否免费   1 免费  2 收费
                        'section_video' => $vo['advVideo'],
                        'section_size' => $vo['section_size'], // 单位K
                        'section_long_time' => $vo['section_long_time'], // 时长  20:15
                        'section_time' => date('Y-m-d H:i:s'),
                        'section_video_updatetime' => date('Y-m-d H:i:s')
                    ];
                    Db::table('up_public_section')->insert($section_insert);
                }
            }
            Db::commit();
            return json(['status' => 200, 'msg' => '发布成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '发布失败']);
        }
    
        
    }
    
    /**
     * 我的题库
     * @return \think\response\Json
     */
    public function myQuestionBank(){
        $list = Db::table('up_question_bank')
            ->field('question_id, question_title, question_time, question_type, question_price')
            ->where('question_from', 1)
            ->where('question_user_id', $this->user_id)
            ->order('question_is_top asc, question_time desc')
            ->page($this->page, 10)
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 新建题库
     * @return \think\response\Json
     */
    public function addQuestionBank(){
        $question = input('question');
        $question = json_decode($question, true);
        $insert = [
            'question_title' => $question['question_title'],
            'question_subject_id' => $question['question_subject'],
            'question_grade_id' => $question['question_grade'],
            'question_time' => date('Y-m-d H:i:s'),
            'question_from' => 1,
            'question_user_id' => $this->user_id
        ];
        // 根据价格判断是否免费
        if($question['question_price']){
            $insert['question_type'] = 2;
            $insert['question_price'] = $question['question_price'];
        }else{
            $insert['question_type'] = 1;
            $insert['question_price'] = 0;
        }
        
        Db::startTrans();
        try{
            // 插入题库
            $question_id = Db::table('up_question_bank')->insertGetId($insert);
            
            // 插入题库文件
            if(is_array($question['files']) && !empty($question['files'])){
                foreach($question['files'] as $k => $v){
                    $file_insert = [
                        'q_question_id' => $question_id,
                        'q_file_name' => $v['q_file_name'],
                        'q_file_url' => $v['q_file_url'],
                        'q_file_dis' => $v['q_file_dis'],
                        'q_file_size' => $v['q_file_size'],
                        'q_file_time' => date('Y-m-d H:i:s')
                    ];
                    Db::table('up_question_file')->insert($file_insert);
                }
            }
            Db::commit();
            return json(['status' => 200, 'msg' => '发布成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '发布失败']);
        }
    }
    
    /**
     * 题库详情
     * @return \think\response\Json
     * question_id     题库ID
     * page            页码
     */
    public function questionBankDetail(){
        $question_id = input('question_id');
        $detail = Db::table('up_question_bank')
            ->where('question_id', $question_id)
            ->field('question_title, question_time, question_type, question_price')
            ->find();
        if($detail){
            $files = Db::table('up_question_file')
                ->where('q_question_id', $question_id)
                ->field('q_file_id, q_file_url, q_file_name, q_file_dis, q_file_size')
                ->order('q_file_time desc')
                ->page($this->page, 10)
                ->select();
            if($files){
                $detail['files'] = $files;
            }else{
                $detail['files'] = [];
            }
            return json(['status' => 200, 'msg' => $detail]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 常见问题列表
     * @return \think\response\Json
     */
    public function problem(){
        $list = Db::table('up_problem')
            ->field('problem_id, problem_describe')
            ->order('create_time desc')
            ->where('is_end', 2)
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 常见问题详情
     * @return \think\response\Json
     * problem_id   问题ID
     */
    public function problemDetail(){
        $problem_id = input('problem_id');
        $detail = Db::table('up_problem')->where('problem_id', $problem_id)->value('problem_answer');
        if($detail){
            return json(['status' => 200, 'msg' => $detail]);
        }else{
            return json(['status' => 200, 'msg' => '']);
        }
    }
    
    /**
     * 问题反馈分类
     * @return \think\response\Json
     */
    public function feedbackType(){
        $cate = Db::table('up_feedback_type')->select();
        if($cate){
            return json(['status' => 200, 'msg' => $cate]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 举报操作
     * @return \think\response\Json
     * type     举报类型
     * content  举报内容
     * img      举报图片
     * phone    联系方式
     */
    public function doFeedback(){
        $type = input('type');
        $content = input('content');
        $img = input('img');
        $phone = input('phone');
        $insert = [
            'f_user_id' => $this->user_id,
            'feedback_phone' => $phone,
            'feedback_content' => $content,
            'feedback_time' => date('Y-m-d H:i:s'),
            'feedback_end' => 2,
            'feedback_type_id' => $type,
            'feedback_img' => $img
        ];
        $res = Db::table('up_feedback')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '反馈成功']);
        }else{
            return json(['status' => 0, 'msg' => '反馈失败']);
        }
    }
    
    /**
     * 功能介绍
     * @return \think\response\Json
     */
    public function studentAbout(){
        $about = Db::table('up_about')
            ->where('about_id', 4)
            ->field('about_teacher_gn')
            ->find();
        return json(['status' => 200, 'msg' => $about]);
    }
    
    /**
     * 法律声明
     * @return \think\response\Json
     */
    public function studentAboutF(){
        $about = Db::table('up_about')
            ->where('about_id', 5)
            ->field('about_teacher_law')
            ->find();
        return json(['status' => 200, 'msg' => $about]);
    }
    
    /**
     * 平台协议
     * @return \think\response\Json
     */
    public function studentAboutP(){
        $about = Db::table('up_about')
            ->where('about_id', 6)
            ->field('about_teacher_platform')
            ->find();
        return json(['status' => 200, 'msg' => $about]);
    }
    
    /**
     * 联系我们
     * @return \think\response\Json
     */
    public function contactUs(){
        $phone = Db::table('up_about')->where('about_id', 7)->value('about_phone');
        if($phone){
            return json(['status' => 200, 'msg' => $phone]);
        }else{
            return json(['status' => 200, 'msg' => '']);
        }
    }
    
    /**
     * 系统消息列表
     * @return \think\response\Json
     * page   页码
     */
    public function systemNoticeList(){
        $where['n_range'] = 2; // 老师端的系统信息
        $where['n_del_user_id'] = ['NOTLIKE', '%' . ',' . $this->user_id . ',' . '%']; // 未删除
        $where['n_type'] = 1; // 系统消息
        $where['n_user_id'] = ['LIKE', '%' . ',' . $this->user_id . ',' . '%']; // 属于我的
        $noticeList = Db::table('up_notice')
            ->where($where)
            ->field('n_id, n_title, n_send_datetime, n_read_user_id, n_content')
            ->order('n_send_datetime desc')
            ->page($this->page, 10)
            ->select();
        if($noticeList){
            // 已读、未读
            foreach($noticeList as $k => $v){
                $readed = explode(',', trim($v['n_read_user_id'], ','));
                if(in_array($this->user_id, $readed)){
                    $noticeList[$k]['is_read'] = 1; // 已读
                }else{
                    $noticeList[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $noticeList]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 系统消息详情
     * @return \think\response\Json
     * n_id    消息ID
     */
    public function systemNoticeDetail(){
        $n_id = input('n_id');
        // 消息详情
        $info = Db::table('up_notice')
            ->field('n_read_user_id, n_title, n_content, n_send_datetime')
            ->where('n_id', $n_id)
            ->find();
        if($info){
            // 修改未读状态
            $readed = explode(',', trim($info['n_read_user_id'], ','));
            if(!in_array($this->user_id, $readed)){
                $read_user = ',' . implode(',', $readed) . ',' . $this->user_id . ',';
                $res = Db::table('up_notice')->where('n_id', $n_id)->setField('n_read_user_id', $read_user);
                if($res){
                    return json(['status' => 200, 'msg' => $info]);
                }else{
                    return json(['status' => 0, 'msg' => '操作数据库失败']);
                }
            }else{
                return json(['status' => 200, 'msg' => $info]);
            }
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 清空系统消息
     * @return \think\response\Json
     */
    public function systemNoticeDel(){
        // 所有属于我的，没有被删除的消息
        $where['n_range'] = 2; // 学生端的系统信息
        $where['n_del_user_id'] = ['NOTLIKE', '%' . ',' . $this->user_id . ',' . '%']; // 未删除
        $where['n_type'] = 1; // 系统消息
        $where['n_user_id'] = ['LIKE', '%' . ',' . $this->user_id . ',' . '%']; // 属于我的
        $all = Db::table('up_notice')
            ->where($where)
            ->column('n_id, n_del_user_id');
        if($all){
            Db::startTrans();
            try{
                foreach($all as $k => $v){
                    $del = $v . $this->user_id . ',';
                    Db::table('up_notice')->where('n_id', $k)->setField('n_del_user_id', $del);
                }
                $res = 1;
                Db::commit();
            }catch(\Exception $e){
                $res = 2;
                Db::rollback();
            }
            if($res == 1){
                return json(['status' => 200, 'msg' => '清空成功']);
            }else{
                return json(['status' => 0, 'msg' => '清空失败']);
            }
        }else{
            return json(['status' => 0, 'msg' => '暂无数据']);
        }
    }
    
    /**
     * 通知消息列表
     * @return \think\response\Json
     * page   页码
     */
    public function informNoticeList(){
        $where['n_range'] = 2; // 老师端的系统信息
        $where['n_del_user_id'] = ['NOTLIKE', '%' . ',' . $this->user_id . ',' . '%']; // 未删除
        $where['n_type'] = 2; // 通知消息
        $where['n_user_id'] = $this->user_id; // 属于我的
        $noticeList = Db::table('up_notice')
            ->where($where)
            ->field('n_id, n_title, n_send_datetime, n_read_user_id, n_subject_type, n_subject_id, n_content')
            ->order('n_send_datetime desc')
            ->page($this->page, 10)
            ->select();
        if($noticeList){
            // 已读、未读
            foreach($noticeList as $k => $v){
                $readed = explode(',', trim($v['n_read_user_id'], ','));
                if(in_array($this->user_id, $readed)){
                    $noticeList[$k]['is_read'] = 1; // 已读
                }else{
                    $noticeList[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $noticeList]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 通知消息详情
     * @return \think\response\Json
     * n_id    消息ID
     */
    public function informNoticeDetail(){
        $n_id = input('n_id');
        // 消息详情
        $info = Db::table('up_notice')
            ->field('n_read_user_id, n_title, n_content, n_send_datetime, n_content')
            ->where('n_id', $n_id)
            ->find();
        if($info){
            // 修改未读状态
            $readed = explode(',', trim($info['n_read_user_id'], ','));
            if(!in_array($this->user_id, $readed)){
                $read_user = ',' . implode(',', $readed) . ',' . $this->user_id . ',';
                $res = Db::table('up_notice')->where('n_id', $n_id)->setField('n_read_user_id', $read_user);
                if($res){
                    return json(['status' => 200, 'msg' => $info]);
                }else{
                    return json(['status' => 0, 'msg' => '操作数据库失败']);
                }
            }else{
                return json(['status' => 200, 'msg' => $info]);
            }
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 清空通知消息
     * @return \think\response\Json
     */
    public function informNoticeDel(){
        // 所有属于我的，没有被删除的消息
        $where['n_range'] = 2; // 老师端的系统信息
        $where['n_del_user_id'] = ['NOTLIKE', '%' . ',' . $this->user_id . ',' . '%']; // 未删除
        $where['n_type'] = 2; // 通知消息
        $where['n_user_id'] = $this->user_id; // 属于我的
        $all = Db::table('up_notice')
            ->where($where)
            ->column('n_id, n_del_user_id');
        if($all){
            Db::startTrans();
            try{
                foreach($all as $k => $v){
                    $del = $v . $this->user_id . ',';
                    Db::table('up_notice')->where('n_id', $k)->setField('n_del_user_id', $del);
                }
                $res = 1;
                Db::commit();
            }catch(\Exception $e){
                $res = 2;
                Db::rollback();
            }
            if($res == 1){
                return json(['status' => 200, 'msg' => '清空成功']);
            }else{
                return json(['status' => 0, 'msg' => '清空失败']);
            }
        }else{
            return json(['status' => 0, 'msg' => '暂无数据']);
        }
    }
    
    /**
     * 审核回显
     * @return \think\response\Json
     */
    public function showCert(){
        $cert = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_subject, user_grade, user_honor_img, user_good_at, user_tcahe_style, user_toll, 
            user_front_img, user_back_img, user_zige_img, reject, user_status')
            ->find();
        if($cert){
            if($cert['user_honor_img']){
                $img_arr = explode(',', trim($cert['user_honor_img'], ','));
                $cert['user_honor_img'] = $img_arr;
            }else{
                $cert['user_honor_img'] = [];
            }
            return json(['status' => 200, 'msg' => $cert]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 提交提现申请
     * @return \think\response\Json
     * type                 提现方式    1 微信   2 支付宝
     * withdraw_zh          微信/支付宝账号
     * withdraw_rel_name    真实姓名
     * withdraw_money       提现金额
     * withdraw_openid      微信提现时的微信唯一标识
     */
    public function tiXian(){
        $type = input('type'); // 提现方式  1 微信   2 支付宝
        $withdraw_zh = input('withdraw_zh'); // 支付宝/微信账号
        $withdraw_rel_name = input('withdraw_rel_name'); // 真实姓名
        $withdraw_money = input('withdraw_money'); // 提现金额
        //最低提现金额
        $min_price = Db::table('up_about')->where('about_id', 17)->value('about_tixian_price');
        if($withdraw_money < $min_price) return json(['status' => 0, 'msg' => '最小提现金额不得低于' . $min_price . '元']);
        
        // 判断用户余额
        $user_info = Db::table('up_u_user')
            ->field('user_balance')
            ->where('user_id', $this->user_id)
            ->find();
        if($withdraw_money > $user_info['user_balance']) return json(['status' => 0, 'msg' => '余额不足']);
        
        // 提现后账户余额
        $min_balance = Db::table('up_about')->where('about_id', 18)->value('about_balance');
        $balance = $user_info['user_balance'] - $withdraw_money;
        if($balance < $min_balance) return json(['status' => 0, 'msg' => '账户余额不得低于' . $min_balance . '元']);

        //***********  第三方平台限制  ***********//
        // 支付宝最低提现金额为0.1元
        if($type == 2){
            if($withdraw_money < 0.1){
                return json(['status' => 0, 'msg' => '支付宝最低提现金额为0.1元']);
            }
        }
        // 微信最低提现金额为1元
        if($type == 1){
            if($withdraw_money < 1){
                return json(['status' => 0, 'msg' => '微信最低提现金额为1元']);
            }
        }
        //***********  第三方平台限制  ***********//
        
        Db::startTrans();
        try{
            // 插入提现申请表
            $insert = [
                'withdraw_user_id' => $this->user_id,
                'withdraw_type' => $type,
                'withdraw_zh' => $withdraw_zh,
                'withdraw_rel_name' => $withdraw_rel_name,
                'withdraw_money' => $withdraw_money,
                'withdraw_create_time' => date('Y-m-d H:i:s'),
                'withdraw_num' => date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT)
            ];
            if($type == 1){
                $insert['withdraw_openid'] = input('withdraw_openid');
            }
            Db::table('up_withdraw')->insert($insert);
            
            // 扣除用户余额
            Db::table('up_u_user')->where('user_id', $this->user_id)->setDec('user_balance', $withdraw_money);
            Db::commit();
            return json(['status' => 200, 'msg' => '提交成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 公开课详情
     * @return \think\response\Json
     * public_id      公开课ID
     */
    public function publicDetail(){
        $public_id = input('public_id');
        $info = Db::table('up_public_class')
            ->where('public_id', $public_id)
            ->field('public_name, public_type, public_price')
            ->find();
        $count = Db::table('up_public_apply')->where('apply_public_id', $public_id)->count();
        $info['num'] = $count;
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 课程简介和老师简介
     * @return \think\response\Json
     * public_id     课程ID
     */
    public function publicIntro(){
        $public_id = input('public_id');
        $intro = Db::table('up_public_class')
            ->join('up_u_user', 'public_teacher_id = user_id', 'left')
            ->join('up_grade', 'user_grade = grade_id', 'left')
            ->field('public_intro, user_username, user_image, user_gender, grade_name, user_intro, user_id')
            ->where('public_id', $public_id)
            ->find();
        // 星级
        $star = $this->teacherStar($intro['user_id']);
        $intro['star'] = $star;
        return json(['status' => 200, 'msg' => $intro]);
    }
    
    /**
     * 公开课节
     * @return \think\response\Json
     * public_id    公开课ID
     */
    public function publicCatalogue(){
        $public_id = input('public_id');
        // 节
        $section = Db::table('up_public_section')
            ->field('section_id, section_name, section_type, section_video')
            ->where('section_public_id', $public_id)
            ->page($this->page, 10)
            ->order('section_time asc')
            ->select();
        if($section){
            return json(['status' => 200, 'msg' => $section]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 公开课节详情
     * @return \think\response\Json
     * public_id     章ID
     */
    public function publicSection(){
        $public_id = input('public_id');
        $section = Db::table('up_public_section')
            ->where('section_chapter_id', $public_id)
            ->field('section_id, section_name, section_video, section_long_time')
            ->order('section_time asc')
            ->select();
        return json(['status' => 200, 'msg' => $section]);
    }
    
    /**
     * 用于判断用户当天是否有添加视频
     * @return \think\response\Json
     */
    public function todayHaveVideo(){
        // 我的所有的公开课ID
        $public_id_all = Db::table('up_public_class')
            ->where('public_teacher_id', $this->user_id)
            ->column('public_id');
        // 该老师当天有没有发布视频
        $start_time = date('Y-m-d 00:00:00');
        $end_time = date('Y-m-d 23:59:59');
        $res = Db::table('up_public_section')
            ->where('section_public_id', 'IN', $public_id_all)
            ->where('section_video_updatetime', 'between', [$start_time, $end_time])
            ->find();
        if($res){ // 有当天发布的视频
            $is_have = 1;
        }else{ // 没有当天发布的视频
            $is_have = 2;
        }
        return json(['status' => 200, 'msg' => $is_have]);
    }
    
    /**
     * 添加节   用于编辑公开课时
     * @return \think\response\Json
     * public_id           公开课ID
     * section_name        节名称
     * section_type        是否收费   1 免费  2 收费
     * section_video       视频
     * section_size        视频大小   单位K
     * section_long_time   时长  20:15
     */
    public function createSection(){
        $section_public_id = input('public_id'); // 公开课ID
        $section_name = input('section_name'); // 节名称
        $section_type = input('section_type'); // 是否免费   1 免费   2 收费
        $section_video = input('section_video'); // 视频
        $section_size = input('section_size'); // 单位K
        $section_long_time = input('section_long_time'); // 时长 20:15
        
        $insert = [
            'section_public_id' => $section_public_id,
            'section_name' => $section_name,
            'section_type' => $section_type,
            'section_video' => $section_video,
            'section_size' => $section_size,
            'section_long_time' => $section_long_time,
            'section_time' => date('Y-m-d H:i:s'),
            'section_video_updatetime' => date('Y-m-d H:i:s')
        ];
        $section_id = Db::table('up_public_section')->insertGetId($insert);
        if($section_id){
            return json(['status' => 200, 'msg' => $section_id]);
        }else{
            return json(['status' => 0, 'msg' => '添加失败']);
        }
    }
    
    /**
     * 编辑节
     * @return \think\response\Json
     * public_id           公开课ID
     * section_id          节ID
     * section_name        节名称
     * section_type        是否收费   1 免费  2 收费
     * section_video       视频
     * section_size        视频大小   单位K
     * section_long_time   时长  20:15
     * is_update_video     是否更新了视频     1 更新    2 没更新
     */
    public function updateSection(){
        $section_public_id = input('public_id'); // 公开课ID
        $section_id = input('section_id'); // 节ID
        $section_name = input('section_name'); // 节名称
        $section_type = input('section_type'); // 是否免费   1 免费   2 收费
        $section_video = input('section_video'); // 视频
        $section_size = input('section_size'); // 单位K
        $section_long_time = input('section_long_time'); // 时长 20:15
        $is_update_video = input('is_update_video');
        
        $update = [
            'section_public_id' => $section_public_id,
            'section_name' => $section_name,
            'section_type' => $section_type,
            'section_video' => $section_video,
            'section_size' => $section_size,
            'section_long_time' => $section_long_time,
        ];
        if($is_update_video == 1){
            $update['section_video_updatetime'] = date('Y-m-d H:i:s');
        }
        
        $res = Db::table('up_public_section')->where('section_id', $section_id)->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '编辑成功']);
        }else{
            return json(['status' => 0, 'msg' => '编辑失败']);
        }
    }
    
    /**
     * 删除公开课
     * @return \think\response\Json
     * public_id     公开课ID
     */
    public function delPublicClass(){
        $public_id = input('public_id');
        $res = Db::table('up_public_class')->where('public_id', $public_id)->delete();
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 删除节
     * @return \think\response\Json
     * section_id    章ID
     */
    public function delSection(){
        $section_id = input('section_id');
        $res = Db::table('up_public_section')->where('section_id', $section_id)->delete();
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 显示节信息
     * @return \think\response\Json
     * section_id    节ID
     */
    public function showSection(){
        $section_id = input('section_id');
        $section = Db::table('up_public_section')
            ->field('section_id, section_chapter_id, section_name, section_type, section_video, 
            section_size, section_long_time')
            ->where('section_id', $section_id)
            ->find();
        $section['section_size'] = round($section['section_size']/1024, 2);
        return json(['status' => 200, 'msg' => $section]);
    }
    
    /**
     * 根据学生ID获取学生信息
     * @return \think\response\Json
     * other_user_id    学生ID
     */
    public function getOtherInfo(){
        $other_user_id = input('other_user_id');
        $info = Db::table('up_u_user')
            ->where('user_id', $other_user_id)
            ->field('user_username, user_image')
            ->find();
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 返回市ID 用于定位
     * @return \think\response\Json
     */
    public function getShiId(){
        $shi = input('shi');
        $id = Db::table('up_area')->where('district', $shi)->value('district_id');
        if($id){
            return json(['status' => 200, 'msg' => $id]);
        }else{
            return json(['status' => 200, 'msg' => '定位失败']);
        }
    }
    
    /**
     * 用于首页展示小红点
     * @return \think\response\Json
     */
    public function redPoint(){
        $where['n_range'] = 2;
        $where['n_read_user_id'] = ['notlike', '%,' . $this->user_id . ',%'];
        $where['n_del_user_id'] = ['notlike', '%,' . $this->user_id . ',%'];
        $noticeList = Db::table('up_notice')
            ->where($where)
            ->where(function ($query){
                $query->where('n_user_id', $this->user_id)
                    ->whereOr('n_user_id', 'like', '%,' . $this->user_id . ',%');
            })
            ->count();
//            ->select();
        if($noticeList > 0){ // 显示红点(兼容旧版本)
            $red = 1;
        }else{ // 不显示红点
            $red = 2;
        }
//        return json(['status' => 200, 'msg' => $red]);
        return json(['status' => 200, 'msg' => $red, 'num' => $noticeList]);
    }
    
    /**
     * 展示小红点---系统消息
     * @return \think\response\Json
     */
    public function redPointSys(){
        $where['n_range'] = 2;
        $where['n_type'] = 1;
        $where['n_read_user_id'] = ['notlike', '%,' . $this->user_id . ',%'];
        $where['n_del_user_id'] = ['notlike', '%,' . $this->user_id . ',%'];
        $where['n_user_id'] = ['like', '%,' . $this->user_id . ',%'];
        $noticeList = Db::table('up_notice')
            ->where($where)
            ->select();
        if($noticeList){ // 显示红点
            $red = 1;
        }else{ // 不显示红点
            $red = 2;
        }
        return json(['status' => 200, 'msg' => $red]);
    }
    
    /**
     * 展示小红点---通知消息
     * @return \think\response\Json
     */
    public function redPointInform(){
        $where['n_range'] = 2;
        $where['n_type'] = 2;
        $where['n_read_user_id'] = ['notlike', '%,' . $this->user_id . ',%'];
        $where['n_del_user_id'] = ['notlike', '%,' . $this->user_id . ',%'];
        $where['n_user_id'] = $this->user_id;
        $noticeList = Db::table('up_notice')
            ->where($where)
            ->select();
        if($noticeList){ // 显示红点
            $red = 1;
        }else{ // 不显示红点
            $red = 2;
        }
        return json(['status' => 200, 'msg' => $red]);
    }
    
    /**
     * 老师隐私权政策
     * @return \think\response\Json
     */
    public function yinsi(){
        $yinsi = Db::table('up_about')->where('about_id', 10)->value('about_teacher_yinsi');
        return json(['status' => 200, 'msg' => $yinsi]);
    }
    
    /**
     * 老师服务协议
     * @return \think\response\Json
     */
    public function serve(){
        $serve = Db::table('up_about')->where('about_id', 12)->value('about_teacher_serve');
        return json(['status' => 200, 'msg' => $serve]);
    }
    
    /**
     * 生成邀请码
     * @return \think\response\Json
     */
    public function createInviteCode(){
        $user_info = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('userid, user_my_code')
            ->find();
        if($user_info['user_my_code']){
            return json(['status' => 200, 'msg' => $user_info['user_my_code']]);
        }else{
            $res = Db::table('up_u_user')
                ->where('user_id', $this->user_id)
                ->update(['user_my_code' => $user_info['userid']]);
            if($res){
                return json(['status' => 200, 'msg' => $user_info['userid']]);
            }else{
                return json(['status' => 0, 'msg' => '生成邀请码失败']);
            }
        }
    }
    
    /**
     * 我的邀请
     * @return \think\response\Json
     * page    页码
     */
    public function myInvite(){
        $invite = [];
        
        // 我的邀请码
        $my_invite_code = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->value('user_my_code');
//        $invite['invite_code'] = $my_invite_code;
        
        // 我邀请的老师
        if(!empty($my_invite_code)){
            $invited_teacher = Db::table('up_u_user')
                ->where('user_invite_code', $my_invite_code)
                ->field('user_id, user_username, user_image, user_time, user_status')
                ->order('user_time desc')
                ->page($this->page, 10)
                ->select();
            if($invited_teacher){
                $invite['invited_teacher'] = $invited_teacher;
            }else{
                $invite['invited_teacher'] = [];
            }
        }else{
            $invite['invited_teacher'] = [];
        }
        
        // 邀请收益
        $ear = Db::table('up_ear')->where('ear_invite_user_id', $this->user_id)->sum('ear_num');
        $invite['ear'] = $ear;
        return json(['status' => 200, 'msg' => $invite]);
    }
    
    /**
     * 我邀请的老师的提现记录
     * @return \think\response\Json
     * teacher_id    我邀请的老师的ID
     * page          页码
     */
    public function myInviteTeacherWithdraw(){
        $teacher_id = input('teacher_id');
        $withdraw = Db::table('up_withdraw')
            ->field('withdraw_money, withdraw_num, withdraw_att_time')
            ->where('withdraw_user_id', $teacher_id)
            ->where('withdraw_status', 2)
            ->order('withdraw_att_time desc')
//            ->page($this->page, 10)
            ->select();
        if($withdraw){
            return json(['status' => 200, 'msg' => $withdraw]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 提现说明
     * @return \think\response\Json
     */
    public function tixianExplain(){
        $explain = Db::table('up_about')->where('about_id', 16)->value('about_tixian_explain');
        if($explain){
            return json(['status' => 200, 'msg' => $explain]);
        }else{
            return json(['status' => 0, 'msg' => []]);
        }
    }
    
    /**
     * 省市区字符串
     * @param $sheng
     * @param $shi
     * @param $qu
     * @return string
     */
    private function areaStr($sheng, $shi, $qu)
    {
        $shengName = Db::name('area')->where('district_id', $sheng)->value('district');
        $shiName = Db::name('area')->where('district_id', $shi)->value('district');
        $quName = Db::name('area')->where('district_id', $qu)->value('district');
        $str = $shengName . ' ' . $shiName . ' ' . $quName;
        return $str;
    }
    
    /**
     * 注销（冻结）
     * @return \think\response\Json
     */
    public function logout(){
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->setField('user_status', 4);
        if($res){
            return json(['status' => 200, 'msg' => '注销成功']);
        }else{
            return json(['status' => 0, 'msg' => '注销失败']);
        }
    }
    
    /**
     * 老师学历
     * @return \think\response\Json
     */
    public function userEdu(){
        $edu = Db::table('up_edu')->field('edu_id, edu_name')->select();
        return json(['status' => 200, 'msg' => $edu]);
    }
    
    /**
     * 邀请码小写转大写
     */
    public function todaxie(){
        $list = Db::table('up_u_user')
            ->where('user_my_code', 'neq', '')
//            ->whereOr('user_invite_code', 'neq', '')
            ->field('user_id, user_my_code, user_invite_code, userid')
            ->select();
        foreach($list as $k => $v){
            if(!empty($v['user_my_code'])){
                Db::table('up_u_user')
                    ->where('user_invite_code', $v['user_my_code'])
                    ->update(['user_invite_code' => $v['userid']]);
                Db::table('up_u_user')
                    ->where('user_id', $v['user_id'])
                    ->update(['user_my_code' => $v['userid']]);
            }
        }
    }
}